package com.ludomancy.starwave;

import java.util.Comparator;
import java.util.Iterator;
import java.util.Vector;

public class AttackComparator implements Comparator
{
	private Vector attacks;
	
	public AttackComparator(Vector attacks)
	{
		this.attacks = attacks;
	}
	
	private boolean isAttacked(Ship victim)
	{
		for (Iterator iter = attacks.iterator(); iter.hasNext();)
		{
			AttackAnimation att = (AttackAnimation) iter.next();
			if(att.enemies.contains(victim))
			{
				return true;
			}
		}
		return false;
	}
	
	public int compare(Object o1, Object o2)
	{
		AttackAnimation att1 = (AttackAnimation) o1;
		AttackAnimation att2 = (AttackAnimation) o2;
		
//		if(att1.attacker.owner == att2.attacker.owner)
//		{
//			return 0;
//		}
		// both ships belong to different players
//		if(att1.enemies.isEmpty() || att2.enemies.isEmpty())
//		{
//			return 0;
//		}
		// both ships are attackers
		boolean ship1Attacked = isAttacked(att1.attacker);
		boolean ship2Attacked = isAttacked(att2.attacker);
		if(ship1Attacked && ship2Attacked)
		{
			return new Integer(att1.attacker.py).compareTo( new Integer(att2.attacker.py));
		}
		else
		if(ship1Attacked)
		{
			return -1;
		}
		else
		if(ship2Attacked)
		{
			return 1; 
		}
		else
		{
			// none is attacked
			return 0;
		}
	}
}
